package com.smart.draw.plantuml.service;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriUtils;

import java.nio.charset.StandardCharsets;

/**
 * @Author xiaoqiang
 * @Version UrlService.java, v 0.1 2025年08月31日 11:33 xiaoqiang
 * @Description: TODO
 */
@Service
public class UrlService {

    public String generatePngUrl(HttpServletRequest request, String encodedSource) {
        String encoded = UriUtils.encodePathSegment(encodedSource, StandardCharsets.UTF_8);

        // 使用 ServletUriComponentsBuilder 构造完整 URL
        String generatedUrl = ServletUriComponentsBuilder
                .fromContextPath(request)
                .path("/png/{source}")
                .buildAndExpand(encoded)
                .toUriString();

        return generatedUrl;
    }
}
